#include "type.h"
#include "message.h"
#include "protect.h"
#include "proc.h"
void cli();
void delay_mill(u32);
void dispStr(char*);
void init_prot(void);
void init_descriptor(struct descriptor*,u32,u32,u8,u8);
void memset(u32*,u32,u8);
u32 seg2phys(struct descriptor*);
void init_clock();
void init_keyboard();
void io_delay();
u32	 get_char1();
//void interrupt(u32 dpid);
//oid sendrecv2(Message* msg);
void testA();
void testB();
void StackTop();
u32		get_code(void);
void	dispChar(char);
void send2(Message* msg);
void receive(Message* msg);
unsigned char task_stack[STACK_SIZE_TOTAL];
struct	process	p_process[MAX_NUM];
struct	process* cur=p_process;
void restart();
extern k_reenter;
static char* name[]={"KE","AP","BP"};
void cstart(){
	int	i;
	struct task* t=task_tab;
	dispStr("hello! kernel Start!\n");
	for(i=0;i<MAX_NUM;t++,cur++,i++){
		init_descriptor(&gdt[6+i],vir2phys(seg2phys(&gdt[1]),
		cur->ldt),2*sizeof(struct descriptor)-1,DA_LDT,0);
		/*
		*服务进程优先级为2
		*/
		if(t->type==SERVER){
			cur->frame.cs=0|SA_LDT|SA_RPL2;
			cur->frame.ds=8|SA_LDT|SA_RPL2;
			cur->frame.es=8|SA_LDT|SA_RPL2;
			cur->frame.fs=8|SA_LDT|SA_RPL2;
			cur->frame.ss=8|SA_LDT|SA_RPL2;
			init_descriptor(&cur->ldt[0],0,0xffff,DA_CR|DA_DPL2,DA_LIMIT_4K|DA_32);
			init_descriptor(&cur->ldt[1],0,0xffff,DA_DRW|DA_DPL2,DA_LIMIT_4K|DA_32);
			cur->frame.gs=24|SA_GDT|SA_RPL2;
		}else{			
		/*
		*用户进程优先级为3
		*/
			cur->frame.cs=0|SA_LDT|SA_RPL3;
			cur->frame.ds=8|SA_LDT|SA_RPL3;
			cur->frame.es=8|SA_LDT|SA_RPL3;
			cur->frame.fs=8|SA_LDT|SA_RPL3;
			cur->frame.ss=8|SA_LDT|SA_RPL3;
			init_descriptor(&cur->ldt[0],0,0xffff,DA_CR|DA_DPL3,DA_LIMIT_4K|DA_32);
			init_descriptor(&cur->ldt[1],0,0xffff,DA_DRW|DA_DPL3,DA_LIMIT_4K|DA_32);
			cur->frame.gs=24|SA_GDT|SA_RPL3;
		}	
		cur->frame.esp=(u32)task_stack+1024*(i+1);
		cur->frame.eip=t->eip;
		cur->frame.eflags=0x1202;
		cur->ldt_selector=48+8*i;
		cur->state=READY;
		cur->name=name[i];
		cur->counter=120;
	}
	cur=p_process;
	init_clock();
	init_keyboard();
//	init_schedule(); //初始化进程调度
	init_clock_schedule();
	dispStr("here is the out\n");
	out_byte(0x21,0xff);
//	dispStr("out_byte\n");
	k_reenter=1; //first start:
	cli();
	restart();
	while(1){}
}
void testA(){
	Message msg;
	char* str_array[]={"hello me","i love you"};
	int i=0;
//	msg.p_dest=2;  
//	msg.m.m1.str="hello me";
	while(1){
//		msg.m.m1.str=str_array[i%2];
	///	msg.p_dest=1;
//		dispStr("the PA msg addr->");
//		disp_int(&msg);
//		dispStr("\n");
//		dispStr("A");
//		dispStr("A");
//		u32 code=get_code();
//		dispChar(code);
//		delay_mill(20);		
	//  	interrupt(1);
//		dispChar(get_char1());
//		msg.p_dest=1;
	//	send2(&msg);
	///	send_recv2(&msg,SEND);
		dispStr("A");
		delay_mill(200);
//		i++;
	}
}
void testB(){
//	while(1){
//		dispStr("B");
//		delay_mill(2000);
//	}
//	Message msg;
//	while(1){
//	delay_mill(2000);
//	dispStr("A");
//	}
	while(1){
//		dispStr("the PB msg addr->");
//		disp_int(&msg);
//		dispStr("\n");
		//recv2(&msg);
	///	send_recv2(&msg,RECEIVE);
//		delay_mill(20);
//		dispStr("\ntestB say hello you\n");
	///	dispStr("-");
	///	dispStr(msg.m.m1.str);
	///	dispStr("-");
	///	msg.p_dest=0;
	//	send2(&msg);	
		dispStr("B");
		delay_mill(200);
	///	send_recv2(&msg,SEND);
	}
}
